<template>
	<view class="">
		<Header title="模特发布" :showBack="true"></Header>
		<view class="image-info">
			<uni-file-picker class="file-list" title="上传模特照片"
			  fileMediatype="image" 
				@select="imageSelect" 
				@success="imageSuccess" 
				@fail="imageFail"
		  :auto-upload="false"
		   v-model="imageList"></uni-file-picker>
			<uni-file-picker class="file-list" title="上传模特视频" :auto-upload="false"  
				@select="videoSelect" 
				@success="videoSuccess" 
				@fail="videoFail"  
				v-model="videoList" file-mediatype="video"></uni-file-picker>
		</view>
		<view class="btn-group">
			<button  plain type="warn" @click="prevFn">上一步</button>
			<button  type="primary" @click="saveFn">保存</button>
		</view>
	</view>
	
</template>

<script>
import Header from '@/components/header.vue'
export default {
	components: {
		Header
	},
	data() {
		return {
			imageList: [], // 照片集合
			videoList: [], // 视频集合
			video: '',
			pic: ''
		};
	},
	onLoad(e) {
		
	  this.mid = e.mid
	  if(e.mid !== ''){
	  	
	  	this.getModelsInfo(e.mid)
	  }
	},
	methods: {
		// 上一步
		prevFn () {
			uni.navigateBack({
				delta: 1
			})
		},
		// 获取配置信息
		async getModelsInfo (mid) {
				
		  const res  = await this.http.httpRequest('/models/getModelsInfo','post',{mid:mid})
		  if (res.data.code == 1) {
			  this.imageList = res.data.data.pic;
			  this.videoList = res.data.data.video
			  // this.$set(this.imageList, 0, res.data.data.pic);
			  // this.$set(this.videoList, 0, res.data.data.video);
			
			 
		  } else {
			  this.$u.toast('配置请求失败')
		  }
		  
		},
		// 保存
		async saveFn () {
			console.log(this.imageList)
			if (this.imageList == '') {
				this.$u.toast('图片不能为空')
				return
			}
			
			const params = {
				mid: this.mid,
				video: this.videoList,
				pic: this.imageList
			}
			const res = await this.http.httpRequest('/models/updateModelsStepTwo','post',params)
		    if (res.data.code == 1) {
				this.$u.toast('发布成功')
				setTimeout(function() {
					uni.navigateTo({
						url: '/pages/model/modelDetail/modelDetail?item=' + res.data.data
					})
				}, 1000);
				
			} else {
				this.$u.toast('发布失败')
			}
		},
		  // 获取上传状态
		imageSelect(e){
			
			let that = this;
			
			for (var i=0;i<e.tempFilePaths.length;i++)
			{ 
				console.log(e.tempFilePaths[i])
			    uni.uploadFile({
			    	url : 'http://lingdian.h8m.cn/index.php/api/models/upload',
			    	filePath: e.tempFilePaths[i],				
			    	name: 'file',
			    	success: function (uploadFileRes) {
						console.log(uploadFileRes.data);
						if(that.imageList == ''){
							that.imageList = uploadFileRes.data
						}else{
							that.imageList = that.imageList+','+uploadFileRes.data
						}
			    		
			    		// that.$set(that.imageList,that.imageList.length,uploadFileRes.data);
			    		
			    	
			    	}
			    });
			}
			
			console.log(that.imageList)
		},
		// 获取上传进度
		imageProgress(e){
			console.log('上传进度：',e)
		},
		// 上传成功
		imageSelectSuccess(e){
			console.log('上传成功')
		},
		imageFail(e){
			console.log('上传成功')
		},
		// 获取上传状态
		videoSelect(e){
			let that = this;
			for (var i=0;i<e.tempFilePaths.length;i++)
			{ 
			    uni.uploadFile({
			    	url : 'http://lingdian.h8m.cn/index.php/api/models/uploadvideo',
			    	filePath: e.tempFilePaths[i],				
			    	name: 'file',
			    	success: function (uploadFileRes) {
						if(that.videoList == ''){
							that.videoList = uploadFileRes.data
						}else{
							that.videoList = that.videoList+','+uploadFileRes.data
						}
			    		
			    		// that.$set(that.imageList,that.imageList.length,uploadFileRes.data);
			    		
			    	
			    	}
			    });
			}
			
			// uni.uploadFile({
			// 	url : 'http://lingdian.h8m.cn/index.php/api/models/uploadvideo',
			// 	filePath: e.tempFilePaths[0],				
			// 	name: 'file',
			// 	success: function (uploadFileRes) {
					
			// 		that.$set(that.videoList,that.videoList.length,uploadFileRes.data);
					
				
			// 	}
			// });
			// console.log(that.videoList)
			
		},
		// 获取上传进度
		videoProgress(e){
			console.log('上传进度：',e)
		},
		// 上传成功
		videoSuccess(e){
			console.log('上传成功')
		},
		videoFail(e){
			console.log('上传成功')
		},

		// 上传失败
		fail(e){
			console.log('上传失败：',e)
		}
	}
};
</script>

<style lang="scss">
	.image-info {
		padding: 20upx;
		.file-list {
			margin-bottom: 40upx;
		}
	}
</style>
